home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro14 / execute.c < prev    next >
Text File  |  1986-04-02  |  2KB  |  72 lines

  1. #include <stdio.h>
  2. #include "bm.h"
  3. #include "Extern.h"
  4. Execute(DescVec, NPats, TextFile, Buffer)
  5. struct PattDesc *DescVec[]; /* pointers to status vectors for the different
  6.     * patterns, including skip tables, position in buffer, etc. */
  7. int NPats; /* number of patterns */
  8. char Buffer[]; /* holds text from file */
  9. int TextFile; /* file to search */
  10. {
  11.     int NRead, /* number of chars read from file */
  12.         BuffSize, /* number of chars in buffer */
  13.         BuffPos, /* offset of first char in Buffer in TextFile */
  14.         BuffEx, /* flag to indicate that buffer has been searched */
  15.         ResSize, /* number of characters in the last, incomplete line */
  16.         Found; /* flag indicates whether pattern found
  17.         * completely and all matches printed */
  18.     char *BuffEnd; /* pointer to last char of last complete line */
  19.  
  20.     /* misc working variables */
  21.     int i;
  22.  
  23.     /* initialize */
  24.     ResSize = 0;
  25.     Found = 0;
  26.     BuffPos = 0;
  27.     for (i=0; i < NPats; i++) {
  28.         DescVec[i] -> Success = 0;
  29.         DescVec[i] -> Start = Buffer;
  30.     } /* for */
  31.     /* now do the searching */
  32.     do {
  33.         /* first, read a bufferfull and set up the variables */
  34.         NRead = read(TextFile,Buffer + ResSize, MAXBUFF-ResSize);
  35.         BuffEx = 0;
  36.         BuffSize = ResSize + NRead;
  37.         BuffEnd = Buffer + BuffSize - 1;
  38.         /* locate the end of the last complete line */
  39.         while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  40.             --BuffEnd;
  41.         if (BuffEnd < Buffer)
  42.             BuffEnd = Buffer + BuffSize - 1;
  43.         while (!BuffEx) { /* work through one buffer full */
  44.             BuffEx = 1; /* set it provisionally, then clear
  45.             * it if we find the buffer non-empty */
  46.             for (i=0; i< NPats; i++) {
  47.                 if (!DescVec[i]->Success)
  48.                 /* if the pattern  has not been found */
  49.                     DescVec[i]-> Success =
  50.                     Search(DescVec[i]->Pattern,
  51.                     DescVec[i]->PatLen, Buffer, BuffEnd,
  52.                     DescVec[i]->Skip1, DescVec[i]->Skip2,
  53.                     DescVec[i]);
  54.                 if (DescVec[i]->Success){
  55.                 /* if a match occurred */
  56.                     BuffEx = 0;
  57.                     Found = 1;
  58.                     if (sFlag) return(!Found);
  59.                     MatchFound(DescVec[i],BuffPos,
  60.                     Buffer, BuffEnd);
  61.                     if (lFlag) return(!Found);
  62.                 } /* if */
  63.             } /* for */
  64.         } /* while */
  65.         if(NRead) {
  66.             ResSize = MoveResidue(DescVec,NPats,Buffer,BuffEnd);
  67.             BuffPos += BuffSize - ResSize;
  68.         } /* if */
  69.     } while (NRead);
  70.     return(!Found);
  71. } /* Execute */
  72.